<template>
  <el-container >
    <el-header class="no-flex" height="auto">
      <el-form inline :model="searchForm" label-width="70px">
        <el-form-item label="字典名称" >
          <el-input v-model="searchForm.name" placeholder="请输入字典名称" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="类型编码" >
          <el-input v-model="searchForm.type" placeholder="请输入类型编码" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="upsearch">
            <span>搜索</span>
          </el-button>
          <el-button type="default" icon="el-icon-RefreshLeft" @click="reset">
            <span>重置</span>
          </el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="primary" icon="el-icon-plus" @click="add" v-auths="['*:*:*','system:dicttype:add']">新增</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <el-table ref="table" row-key="id" @selection-change="selectionChange" :data="tableData">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="字典类型名称" prop="name" width="200"></el-table-column>
        <el-table-column label="分类编码" prop="type"  width="200">
          <template #default="scope">
              <router-link :to="{path:'/system/dict-item',query:{
                type:scope.row.type,
                name:scope.row.name,
                id:scope.row.id
              }}">
                <el-link type="primary" :underline="false">{{scope.row.type}}</el-link>
              </router-link>
          </template>
        </el-table-column>
        <el-table-column label="字典描述" prop="remarks" width="200"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" :formatter="rendererDateTime" min-width="300"></el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="180">
          <template #default="scope">
            <el-button-group>
              <el-button text type="primary" @click="table_edit(scope.row.id)" icon="el-icon-Edit" v-auths="['*:*:*','system:dicttype:update']">编辑</el-button>
              <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="danger" icon="el-icon-delete" v-auths="['*:*:*','system:dicttype:delete']">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
      <el-pagination
        :current-page="searchForm.currentPage"
        :page-size="searchForm.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './components/save.vue'
export default {
  name: 'dict',
  components: {
    saveDialog
  },
  data() {
    return {
      dialog: {
        save: false
      },
      total:0,
      searchForm:{
        current:1,
        size:10,
        name:'',
        type:'',
      },
      selection: [],
      search: {
        keyword: null
      },
      tableData:[],
      enabled:null,
      options:[
        {
          enabled: 1,
          label: '启用',
        },
        {
          enabled: 0,
          label: '停用',
        },

      ]
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 请求数据
    getData(){
      this.$API.system.dict.list.get(this.searchForm).then(res=>{
        this.tableData  = res.data.records;
        this.total =res.data.total
      })
    },
    //切换分页
    handleCurrentChange(e){
      this.searchForm.current = e;
      this.getData()
    },
    // 改变单页显示数量
    handleSizeChange(e){
      this.searchForm.size = e
      this.getData()
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    add(){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('add').setData()
      })
    },
    //编辑
    table_edit(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('edit').setData(id)
      })
    },
    //查看
    // table_show(row){
    //   this.dialog.save = true
    //   this.$nextTick(() => {
    //     this.$refs.saveDialog.open('show').setData(row)
    //   })
    // },
    //删除
    async table_del(row){
      let res = await this.$API.system.dict.delDict.get(row.id)
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    async upsearch(){
      let data= this.searchForm
      let res = await this.$API.system.dict.list.get(data)
      this.tableData = res.data.records;
    },
    //重置功能
    reset(){
      this.searchForm={
        current:1,
        size:10,
        name:'',
        type:'',
      }
      this.tableData=[];
      this.getData()
    },
    // //根据ID获取树结构
    // filterTree(id){
    //   var target = null;
    //   function filter(tree){
    //     tree.forEach(item => {
    //       if(item.id == id){
    //         target = item
    //       }
    //       if(item.children){
    //         filter(item.children)
    //       }
    //     })
    //   }
    //   filter(this.$refs.table.tableData)
    //   return target
    // },
    //本地更新数据
    handleSaveSuccess(data, mode){
      if(mode =='add'){
        this.tableData = []
        this.getData()
      }else if(mode =='edit'){
        this.tableData=[]
        this.getData()
      }
    }
  }
}
</script>

<style scoped>

</style>
